package com.iterator.controller.wechat.tcy;

import com.iterator.bean.web.stone.BaseRespVo;
import com.iterator.bean.wechat.tcy.CartCheckOutVO;
import com.iterator.bean.wechat.tcy.CartDeleteBO;
import com.iterator.bean.wechat.tcy.CartIndexVO;
import com.iterator.bean.wechat.tcy.CheckedBO;
import com.iterator.service.wechat.tcy.CartService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author tean_cy
 * @date 2021/1/12 17:20
 */

@RestController
@RequestMapping("wx/cart")
public class CartController {
    @Autowired
    CartService cartService;

    @RequestMapping("index")
    public BaseRespVo index(){
        CartIndexVO cartIndexVO =cartService.index();
        return BaseRespVo.ok(cartIndexVO);
    }

    @RequestMapping("add")
    public BaseRespVo add(@RequestBody Map map){
        Integer goodsId = (Integer) map.get("goodsId");
        Integer number = (Integer) map.get("number");
        Integer productId = (Integer) map.get("productId");
        // 接收的值为购物车的商品数量
        int date = cartService.add(goodsId,number,productId);
        return BaseRespVo.ok(date);
    }

    // TODO 还没有写
    @RequestMapping("fastadd")
    public BaseRespVo fastAdd(@RequestBody Map map){
        Integer goodsId = (Integer) map.get("goodsId");
        Integer number = (Integer) map.get("number");
        Integer productId = (Integer) map.get("productId");
        int data = cartService.fastAdd(goodsId,number,productId);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody Map map){
        Integer goodsId = (Integer) map.get("goodsId");
        Integer id = (Integer) map.get("id");
        Integer number = (Integer) map.get("number");
        Integer productId = (Integer) map.get("productId");
        int code = cartService.update(goodsId,id,number,productId);
        if (code == 1){
            return BaseRespVo.ok();
        }

        return BaseRespVo.fail("修改失败");
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody CartDeleteBO cartDeleteBO){
        List<Integer> productIds = cartDeleteBO.getProductIds();
        CartIndexVO  cartDeleteVo =cartService.delete(productIds);
        return BaseRespVo.ok(cartDeleteVo);
    }

    @RequestMapping("goodscount")
    public BaseRespVo goodsCount(){
        // data为购物车商品数量
        int date = cartService.goodsCount();
        return BaseRespVo.ok(date);
    }

    @RequestMapping("checked")
    public BaseRespVo checked(@RequestBody CheckedBO checkedBO){

        CartIndexVO cartCheckedVO = cartService.checked(checkedBO);
        return BaseRespVo.ok(cartCheckedVO);
    }

    @RequestMapping("checkout")
    public BaseRespVo checkout(@RequestParam("cartId") Integer cartId,@RequestParam("addressId") Integer addressId,
                               @RequestParam("couponId") Integer couponId,@RequestParam("grouponRulesId") Integer grouponRulesId){

        CartCheckOutVO cartCheckedVO = cartService.checkout(cartId,addressId,couponId,grouponRulesId);
        return BaseRespVo.ok(cartCheckedVO);
    }
}
